local skill = fk.CreateSkill {
  name = "iberia__iron_chain_skill",
}

Fk:loadTranslationTable {
  ["iberia__iron_chain_skill"] = "铁索连环",
  ["#iron_chain_skill-ex_target"] = "铁索连环：可以指定一个额外目标",
  ["#iron_chain_skill-draw"] = "铁索连环：是否令 %src 摸一张牌？"
}

local I = require "packages/iberia/utility"

skill:addEffect("cardskill", {
  prompt = "#iron_chain_skill",
  can_use = Util.CanUse,
  min_target_num = 1,
  max_target_num = 2,
  mod_target_filter = Util.TrueFunc,
  target_filter = Util.CardTargetFilter,
  on_use = function (self, room, cardUseEvent)
    if not I.isRepeller(cardUseEvent.from) then return end

    local ex_targets = cardUseEvent:getExtraTargets()
    if #ex_targets == 0 then return end

    ex_targets = room:askToChoosePlayers(cardUseEvent.from, {
      max_num = 1,
      min_num = 1,
      targets = ex_targets,
      prompt = "#iron_chain_skill-ex_target",
      skill_name = "#iberia__iron_chain",
      cancelable = true,
      no_indicate = true
    })
    if #ex_targets ~= 1 then return end

    cardUseEvent:addTarget(ex_targets[1])
  end,
  on_effect = function(self, room, effect)
    local state = effect.to.chained
    effect.to:setChainState(not state)
    if not state and effect.to.chained and I.isCounsellor(effect.from) then
      if room:askToSkillInvoke(effect.from, { skill_name = "#iberia__iron_chain", prompt = "#iron_chain_skill-draw:"..effect.to.id }) then
        effect.to:drawCards(1, "iberia__iron_chain")
      end
    end
  end,
})

return skill
